const globalIp = getApp().globalData.userIp
const nowTime = getApp().globalData.nowTime
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchResult:false,
    dataColorYear: false,
    dataColorMonth: false,
    year:"年份选择",
    year1:new Date().getFullYear(),
    year2:new Date().getFullYear(),
    month:"月份选择",
    month1:new Date().getMonth() + 1,
    month2:new Date().getMonth() + 1,
    months: [
      { id: 1, name: "1月" },
      { id: 2, name: "2月" },
      { id: 3, name: "3月" },
      { id: 4, name: "4月" },
      { id: 5, name: "5月" },
      { id: 6, name: "6月" },
      { id: 7, name: "7月" },
      { id: 8, name: "8月" },
      { id: 9, name: "9月" },
      { id: 10, name: "10月" },
      { id: 11, name: "11月" },
      { id: 12, name: "12月" },
    ],
    yearFlag: false, //控制综合下拉框是否显示
    monthFlag: false, //控制综合下拉框是否显示
    isShowAll:false,
    nowTime: nowTime,
    projectList:[],
    openId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getProject();
  },

  dataShowYear: function () {//日期下拉
    if (this.data.yearFlag){
      this.setData({
        monthFlag: false,
        dataColorMonth: false,
        yearFlag : !this.data.yearFlag
      })
    }else{
      this.setData({
        monthFlag: false,
        dataColorMonth: false,
        yearFlag: !this.data.yearFlag
      })
    }
  },

  dataShowMonth: function () {//日期下拉
    if (this.data.monthFlag){
      this.setData({
        monthFlag: !this.data.monthFlag,
        dataColorYear: false,
        yearFlag: false
      })
    }else{
      this.setData({
        monthFlag: !this.data.monthFlag,
        dataColorYear: false,
        yearFlag: false
      })
    }
  },

  changeYear1(e) {
    this.setData({
      year1: e.detail.value,
      year: e.detail.value + '年~' + this.data.year2 + '年',
      // yearFlag: false,
      dataColorYear: true
    })
    // this.getProject()
  },

  changeYear2(e) {
    this.setData({
      year2: e.detail.value,
      year: this.data.year1 + '年~' + e.detail.value + '年',
      yearFlag: false,
      dataColorYear: true
    })
    this.getProject()
  },

  changeMonth1(e) {
    this.setData({
      month1: this.data.months[e.detail.value].id,
      month: this.data.months[e.detail.value].id + '月~' + this.data.month2 + '月',
      // monthFlag: false,
      dataColorMonth: true
    })
    // this.getProject()
  },

  changeMonth2(e) {
    this.setData({
      month2: this.data.months[e.detail.value].id,
      month: this.data.month1 + '月~' + this.data.months[e.detail.value].id + '月',
      monthFlag: false,
      dataColorMonth: true
    })
    this.getProject()
  },

   // 统一请求
   getProject(e) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: globalIp + 'home/ordr/showProjectForMonthTQTB',
      data: {
        sdate: that.data.year1,
        smonth: that.data.month1,
        edate: that.data.year2,
        emonth: that.data.month2,
        openId: that.data.openId || wx.getStorageSync('userInfo').openid
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          var data = res.data.jsonObject.data;
          var arr = [];
          var res = [];
          for (var i in data) {
            arr[i] = [];
            for(var j in data[i]) {
              arr[i].push(data[i][j]);
            }
          }
          for (var i = 0; i < arr[0].length; ++i) {
            res[i] = [];
            for (var j = 0; j < arr.length; ++j) {
              res[i][j] = arr[j][i];
            }
          }
          that.setData({
            projectList: res
          })
        } 
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  }

})